﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Ink;
using System.Windows.Controls.Primitives;


namespace UCUTextRecognizer
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    /// 

    
    public partial class UserControl1 : UserControl
    {
        private Grid grid1;        
        private bool isStarted;

        public UserControl1()
        {
            InitializeComponent();
            Start();
        }

        private void changeStatusBar(String text)
        {
            label1.Content = text;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            changeStatusBar(this.getResult());

            
            //canvas.Strokes.Clear();

            

            /*XmlDocument xmlDocument = new XmlDocument();

           foreach (ContextNode childNode in analyzer.RootNode.SubNodes)
            {
                XmlNode childXmlNode = xmlDocument.CreateNode(XmlNodeType.Element, "Child", "");
                string contextNodeType = childNode.GetType().ToString();
                Console.WriteLine(contextNodeType);
            }*/
        }

    public void Start()
        {
            this.isStarted = false;
            analyzer = new InkAnalyzer();

            //analyze.Click += new System.Windows.RoutedEventHandler(analyze_Click);

            canvas.StylusOutOfRange += new System.Windows.Input.StylusEventHandler(button1_Click);
            LinearGradientBrush myBrush = new LinearGradientBrush();
            myBrush.GradientStops.Add(new GradientStop(Colors.White, 0.0));
            myBrush.GradientStops.Add(new GradientStop(Colors.LightBlue, 2.5));


            canvas.Background = myBrush;

            canvas.Strokes.StrokesChanged += new StrokeCollectionChangedEventHandler(Strokes_StrokesChanged);
        }


        void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
        {
            if (e.Added.Count > 0)
            {
                analyzer.AddStrokes(e.Added);
                analyzer.SetStrokesType(e.Added, StrokeType.Writing);
                analyzer.SetStrokesLanguageId(e.Added, 0x0415); // 0x0415 0x0809

            }

            if (e.Removed.Count > 0)
            {
                analyzer.RemoveStrokes(e.Removed);
            }
            
        }

        public String getResult()
        {
            status = analyzer.Analyze();

            if (status != null && status.Successful == true)
                return analyzer.GetRecognizedString();
                  
            return null;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            canvas.Strokes.Clear();
        }

    



    }
}
